package com.wxf.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ProducerConfig {

    public static final String FANOUT_EXCHANGE = "springboot_fanout-exchange";
    public static final String DIRECT_EXCHANGE = "springboot_direct-exchange";
    public static final String TOPIC_EXCHANGE = "springboot_topic-exchange";

    public static final String FANOUT_QUEUE_01 = "springboot_fanout_queue_01";
    public static final String FANOUT_QUEUE_02 = "springboot_fanout_queue_02";
    public static final String DIRECT_QUEUE_INSERT = "springboot_direct_queue_insert";
    public static final String DIRECT_QUEUE_UPDATE = "springboot_direct_queue_update";
    public static final String TOPIC_QUEUE_INSERT = "springboot_topic_queue_insert";
    public static final String TOPIC_QUEUE_ALL = "springboot_topic_queue_all";

    @Bean
    public Exchange fanoutExchange(){
        return ExchangeBuilder.fanoutExchange(FANOUT_EXCHANGE).durable(true).build();
    }
    @Bean
    public Exchange directExchange(){
        return ExchangeBuilder.directExchange(DIRECT_EXCHANGE).durable(true).build();
    }
    @Bean
    public Exchange topicExchange(){
        return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE).durable(true).build();
    }
    @Bean
    public Queue fanoutQueue01(){
        return QueueBuilder.durable(FANOUT_QUEUE_01).build();
    }
    @Bean
    public Queue fanoutQueue02(){
        return QueueBuilder.durable(FANOUT_QUEUE_02).build();
    }
    @Bean
    public Queue directQueueInsert(){
        return QueueBuilder.durable(DIRECT_QUEUE_INSERT).build();
    }
    @Bean
    public Queue directQueueUpdate(){
        return QueueBuilder.durable(DIRECT_QUEUE_UPDATE).build();
    }
    @Bean
    public Queue topicQueueInsert(){
        return QueueBuilder.durable(TOPIC_QUEUE_INSERT).build();
    }
    @Bean
    public Queue topicQueueAll(){
        return QueueBuilder.durable(TOPIC_QUEUE_ALL).build();
    }

    @Bean
    public Binding fanoutExchange01(@Qualifier("fanoutQueue01")Queue queue,
                                    @Qualifier("fanoutExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
    @Bean
    public Binding fanoutExchange02(@Qualifier("fanoutQueue02")Queue queue,
                                    @Qualifier("fanoutExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
    @Bean
    public Binding directExchangeInsert(@Qualifier("directQueueInsert")Queue queue,
                                    @Qualifier("directExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("insert").noargs();
    }
    @Bean
    public Binding directExchangeUpdate(@Qualifier("directQueueUpdate")Queue queue,
                                        @Qualifier("directExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("update").noargs();
    }
    @Bean
    public Binding topicExchangeInsert(@Qualifier("topicQueueInsert")Queue queue,
                                        @Qualifier("topicExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("#.insert").noargs();
    }
    @Bean
    public Binding topicExchangeAll(@Qualifier("topicQueueAll")Queue queue,
                                       @Qualifier("topicExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("test.*").noargs();
    }
}
